package co.com.going.vista;

import co.com.going.logica.ILogicaUsuarioo;
import co.com.going.modelo.Usuarioo;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import org.primefaces.component.commandbutton.CommandButton;
import org.primefaces.component.inputtext.InputText;
import org.primefaces.component.password.Password;

/**
 *
 * @author DILOVE
 */
@ManagedBean(name = "sesionVista")
@RequestScoped
public class SesionVista {
    @EJB
    private  ILogicaUsuarioo iLogicaUsuarioo;
    
    private InputText txtUsuario;
    private Password txtClave;
    private CommandButton btnIngresar;
    
    public SesionVista() {
    }

    public InputText getTxtUsuario() {
        return txtUsuario;
    }

    public void setTxtUsuario(InputText txtUsuario) {
        this.txtUsuario = txtUsuario;
    }

    public Password getTxtClave() {
        return txtClave;
    }

    public void setTxtClave(Password txtClave) {
        this.txtClave = txtClave;
    }

    public CommandButton getBtnIngresar() {
        return btnIngresar;
    }

    public void setBtnIngresar(CommandButton btnIngresar) {
        this.btnIngresar = btnIngresar;
    }
    
    public void funcion_ingresar(){
        FacesContext context = FacesContext.getCurrentInstance();
        ExternalContext extContext = context.getExternalContext();
        String url = "";
        try {
            Long cedula = Long.parseLong(txtUsuario.getValue().toString());
            String clave = txtClave.getValue().toString();
            Usuarioo usrLogueado = iLogicaUsuarioo.ingresar(cedula, clave);
            if(usrLogueado!=null){
                //Iniciar Sesion
                url = extContext.encodeActionURL
             (context.getApplication().getViewHandler().
                     getActionURL(context,
                             "/"+usrLogueado.getNivelA()+"/index.xhtml"));
                extContext.getSessionMap().put("usuario", usrLogueado);
                extContext.redirect(url);
            }
        } catch(NumberFormatException e){
            FacesContext.getCurrentInstance().addMessage("mensaje", new FacesMessage("Cedula es número"));
        }catch (Exception ex) {
            FacesContext.getCurrentInstance().addMessage("mensaje", new FacesMessage(ex.getMessage()));
            Logger.getLogger(SesionVista.class.getName()).log(Level.SEVERE, null, ex);
        }
    
    }
    
    public void funcion_cerrar(){
        try {
            FacesContext context = FacesContext.getCurrentInstance();
            ExternalContext extContext = context.getExternalContext();
            extContext.getSessionMap().remove("usuario");
            String url = extContext.encodeActionURL(
                    context.getApplication().getViewHandler().getActionURL(context, "/index.xhtml"));
            extContext.redirect(url);
        }catch (IOException ex) {
            Logger.getLogger(SesionVista.class.getName()).log(Level.SEVERE, null, ex);
            FacesContext.getCurrentInstance().addMessage("mensaje", new FacesMessage(ex.getMessage()));
        }
    }
    
    
    
}
